package com.springboot.study.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @Description: TODO
 * @Author dongguo
 * @Date 2020/5/29 13:26
 * @Version V1.0
 **/
public class MoneyUtils {


    /**
     * 根据税率 含税金额，获取税额
     *
     * @param taxtRate 税率
     * @param amount   含税金额
     * @return
     */
    public static Long getTaxtAmount(BigDecimal taxtRate, Long amount) {
        if (taxtRate == null || taxtRate == BigDecimal.valueOf(0)) {
            return amount;
        }
        if (amount == null || amount == 0L) {
            return 0L;
        }
        return BigDecimal.valueOf(amount).subtract((BigDecimal.valueOf(amount)
                .divide((BigDecimal.valueOf(1).add(taxtRate)), 4, BigDecimal.ROUND_HALF_UP))).longValue();
    }


    public static String converTaxRate(BigDecimal taxRate) {
        String taxRateStr = "0";
        taxRate = taxRate.multiply(BigDecimal.valueOf(100)).setScale(0, RoundingMode.HALF_DOWN);
        if (taxRate.longValue() != 0L) {
            taxRateStr = taxRate.toString().concat("%");
        }
        return taxRateStr;
    }


    /**
     * 处理 taxRate
     * @param taxRate
     * @return
     */
    public static String handleTaxRate(BigDecimal taxRate) {
        StringBuffer stringBuffer = new StringBuffer("");
        taxRate = taxRate.multiply(BigDecimal.valueOf(100)).setScale(2, RoundingMode.HALF_UP);
        String stringTaxRate = taxRate.toString();
        if(stringTaxRate.contains("00")){
            taxRate = taxRate.setScale(0, RoundingMode.HALF_UP);
        }
        stringBuffer = stringBuffer.append(taxRate.toString()).append("%");
        return stringBuffer.toString();
    }







}
